package org.xbill.DNS;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Generator
{
  private long current;
  public final int dclass;
  public long end;
  public final String namePattern;
  public final Name origin;
  public final String rdataPattern;
  public long start;
  public long step;
  public final long ttl;
  public final int type;

  public Generator(long paramLong1, long paramLong2, long paramLong3, String paramString1, int paramInt1, int paramInt2, long paramLong4, String paramString2, Name paramName)
  {
    if ((paramLong1 < 0L) || (paramLong2 < 0L) || (paramLong1 > paramLong2) || (paramLong3 <= 0L))
      throw new IllegalArgumentException("invalid range specification");
    if (!(supportedType(paramInt1)))
      throw new IllegalArgumentException("unsupported type");
    DClass.check(paramInt2);
    this.start = paramLong1;
    this.end = paramLong2;
    this.step = paramLong3;
    this.namePattern = paramString1;
    this.type = paramInt1;
    this.dclass = paramInt2;
    this.ttl = paramLong4;
    this.rdataPattern = paramString2;
    this.origin = paramName;
    this.current = paramLong1;
  }

  private String substitute(String paramString, long paramLong)
    throws IOException
  {
    int i = 0;
    byte[] arrayOfByte = paramString.getBytes();
    StringBuffer localStringBuffer = new StringBuffer();
    int j = 0;
    int k = arrayOfByte.length;
    if (j >= k)
      return localStringBuffer.toString();
    char c = (char)(0xFF & arrayOfByte[j]);
    if (i != 0)
    {
      localStringBuffer.append(c);
      i = 0;
    }
    while (true)
    {
      int l;
      int i1;
      long l1;
      long l2;
      long l3;
      int i2;
      label444: label471: String str;
      label553: int i3;
      while (true)
      {
        while (true)
        {
          while (true)
            ++j;
          if (c != '\\')
            break;
          if (j + 1 == arrayOfByte.length)
            throw new TextParseException("invalid escape character");
          i = 1;
        }
        if (c != '$')
          break label706;
        i1 = 0;
        l1 = 0L;
        l2 = 0L;
        l3 = 10L;
        i2 = 0;
        if ((j + 1 >= arrayOfByte.length) || (arrayOfByte[(j + 1)] != 36))
          break;
        localStringBuffer.append((char)(0xFF & arrayOfByte[(++j)]));
      }
      if ((j + 1 < arrayOfByte.length) && (arrayOfByte[(j + 1)] == 123))
      {
        int i5 = j + 1;
        if ((i5 + 1 < arrayOfByte.length) && (arrayOfByte[(i5 + 1)] == 45))
        {
          i1 = 1;
          ++i5;
        }
        if (i5 + 1 >= arrayOfByte.length)
          if (i1 != 0)
            l1 = -l1;
        while (true)
        {
          if ((c != ',') || (i5 + 1 >= arrayOfByte.length));
          do
          {
            while (true)
            {
              do
              {
                if (c != ',')
                  break label471;
                if (i5 + 1 != arrayOfByte.length)
                  break label444;
                throw new TextParseException("invalid base");
                c = (char)(0xFF & arrayOfByte[(++i5)]);
              }
              while ((c == ',') || (c == '}'));
              if ((c < '0') || (c > '9'))
                throw new TextParseException("invalid offset");
              c -= '0';
              l1 = l1 * 10L + l;
            }
            l = (char)(0xFF & arrayOfByte[(++i5)]);
          }
          while ((l == 44) || (l == 125));
          if ((l < 48) || (l > 57))
            throw new TextParseException("invalid width");
          l -= 48;
          l2 = l2 * 10L + l;
        }
        int i6 = (char)(0xFF & arrayOfByte[(++i5)]);
        if (i6 == 111)
          l3 = 8L;
        do
          while (true)
          {
            while (true)
            {
              if ((i5 + 1 != arrayOfByte.length) && (arrayOfByte[(i5 + 1)] == 125))
                break label553;
              throw new TextParseException("invalid modifiers");
              if (i6 != 120)
                break;
              l3 = 16L;
            }
            if (i6 != 88)
              break;
            l3 = 16L;
            i2 = 1;
          }
        while (i6 == 100);
        throw new TextParseException("invalid base");
        j = i5 + 1;
      }
      long l4 = paramLong + l1;
      if (l4 < 0L)
        throw new TextParseException("invalid offset expansion");
      if (l3 == 8L)
      {
        str = Long.toOctalString(l4);
        if (i2 != 0)
          str = str.toUpperCase();
        if ((l2 != 0L) && (l2 > str.length()))
          i3 = (int)l2 - str.length();
      }
      while (true)
      {
        int i4;
        while (true)
        {
          while (true)
          {
            while (true)
            {
              i4 = i3 - 1;
              if (i3 > 0)
                break label691;
              localStringBuffer.append(str);
            }
            if (l3 != 16L)
              break;
            str = Long.toHexString(l4);
          }
          str = Long.toString(l4);
        }
        label691: localStringBuffer.append('0');
        i3 = i4;
      }
      label706: localStringBuffer.append(l);
    }
  }

  public static boolean supportedType(int paramInt)
  {
    int i;
    Type.check(paramInt);
    if ((paramInt != 12) && (paramInt != 5) && (paramInt != 39) && (paramInt != 1) && (paramInt != 28) && (paramInt != 2))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  public Record[] expand()
    throws IOException
  {
    ArrayList localArrayList = new ArrayList();
    long l = this.start;
    while (true)
    {
      if (l >= this.end)
        return ((Record[])localArrayList.toArray(new Record[localArrayList.size()]));
      Name localName = Name.fromString(substitute(this.namePattern, this.current), this.origin);
      String str = substitute(this.rdataPattern, this.current);
      localArrayList.add(Record.fromString(localName, this.type, this.dclass, this.ttl, str, this.origin));
      l += this.step;
    }
  }

  public Record nextRecord()
    throws IOException
  {
    Record localRecord;
    if (this.current > this.end)
      localRecord = null;
    while (true)
    {
      return localRecord;
      Name localName = Name.fromString(substitute(this.namePattern, this.current), this.origin);
      String str = substitute(this.rdataPattern, this.current);
      this.current += this.step;
      localRecord = Record.fromString(localName, this.type, this.dclass, this.ttl, str, this.origin);
    }
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("$GENERATE ");
    localStringBuffer.append(this.start + "-" + this.end);
    if (this.step > 1L)
      localStringBuffer.append("/" + this.step);
    localStringBuffer.append(" ");
    localStringBuffer.append(this.namePattern + " ");
    localStringBuffer.append(this.ttl + " ");
    if ((this.dclass != 1) || (!(Options.check("noPrintIN"))))
      localStringBuffer.append(DClass.string(this.dclass) + " ");
    localStringBuffer.append(Type.string(this.type) + " ");
    localStringBuffer.append(this.rdataPattern + " ");
    return localStringBuffer.toString();
  }
}